package com.ganeshkrishnan.globald.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import com.ganeshkrishnan.globald.abstracts.BaseObject;

@Entity
@Table(name = "CivResource", uniqueConstraints = { @UniqueConstraint(columnNames = { "personID", "ResType" }) })
public class CivResource extends BaseObject {
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	public final int			FOOD				= 0;
	public final int			WATER				= 1;
	public final int			HAY					= 2;
	public final int			WOOD				= 3;
	public final int			SILVER				= 4;
	public final int			GOLD				= 5;
	public final int			DOLLAR				= 6;
	public final int			XP					= 7;
	public final int			KARMA				= 8;

	private Long				ID;
	//Number of civilians for each resource
	private int					CivDeployed;
	//availability of resource
	private long				ResAvailable;
	//kind of resource 
	private int					ResType;
	//last update of resource. increment each hour
	private Date				resourceUpdated;
	private Person				person;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getID() {
		return ID;
	}

	public void setID(Long iD) {
		ID = iD;
	}

	@ManyToOne
	@JoinColumn(name = "PersonID", referencedColumnName = "ID")
	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public int getCivDeployed() {
		return CivDeployed;
	}

	public void setCivDeployed(int civDeployed) {
		CivDeployed = civDeployed;
	}

	public long getResAvailable() {
		return ResAvailable;
	}

	public void setResAvailable(long resAvailable) {
		ResAvailable = resAvailable;
	}

	public int getResType() {
		return ResType;
	}

	public void setResType(int resType) {
		ResType = resType;
	}

	@Column(name = "ResUpDate")
	public Date getResourceUpdated() {
		return resourceUpdated;
	}

	public void setResourceUpdated(Date resourceUpdated) {
		this.resourceUpdated = resourceUpdated;
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof CivResource))
			return false;
		CivResource cv = (CivResource) o;
		if (person != null && ((CivResource) o).getPerson() != null
				&& person.getId() != ((CivResource) o).getPerson().getId())
			return false;

		return (cv.ResType == ResType);
	}

	@Override
	public int hashCode() {
		int result;
		result = (person != null ? person.hashCode() : 0);
		result = 31 * result + ResType;
		return result;
	}

	@Override
	public String toString() {
		return "Civilian " + person.getId() + " " + ResType + "/" + ResAvailable;
	}

}
